package com.app.controller;

import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.ComputerVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Controller
@RequestMapping("/api/com")
public class ComputerController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private ComputerService computerService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private PersonService personService;

    @GetMapping("/getComList")//办公设备管理分页查询
    public Result<Object> getComList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("name") String name,
            @RequestParam("type") Integer type,
            @RequestParam(required = false) Integer departmentId,
            @RequestParam(required = false) String personName,
            Integer sortType,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        List<Integer> idList = departmentService.getDepId(user.getDepartmentId());
        Page<ComputerModel> computerModelPage = computerService.getPageComList(page, limit, name, departmentId, personName, idList, type,sortType);
        for (int i = 0; i < computerModelPage.getRecords().size(); i++) {
            DepartmentModel departmentModel = departmentService.getDepByDepId(computerModelPage.getRecords().get(i).getDepartmentId());
            computerModelPage.getRecords().get(i).setDepartmentText(departmentModel.getName());
        }
        if (computerModelPage.getRecords() == null) {
            return Result.error(200, "暂无数据");
        }

        int count = (int) computerModelPage.getTotal();
        List<ComputerModel> comModelList;
        comModelList = computerModelPage.getRecords();
        ComputerVo vo = new ComputerVo();
        vo.setCount(count);
        vo.setList(comModelList);
        return Result.success(vo);
    }

    @PostMapping("/insertCom")//    新增/修改办公设备管理
    public Result<Object> insertCom(@RequestBody ComputerModel computerModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (computerModel.getId() != null && computerModel.getId() > 0) {
            computerModel.setUpdateTime(CommonHelp.getNowTimestamp());
            computerService.update(computerModel);
        } else {
            computerModel.setCreateTime(CommonHelp.getNowTimestamp());
            computerService.insert(computerModel);
        }
        return Result.success(null);
    }

    @GetMapping("/deleteById")//删除办公设备
    public Result<Object> deleteById(
            @RequestParam("id") Integer id,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        ComputerModel comModel = computerService.getComByComId(id);
        if (comModel == null) {
            return Result.error(500, "该设备不存在");
        } else {
            computerService.delete(id);
            return Result.success(null);
        }
    }
//办公设备的批量删除
    @DeleteMapping("/deleteComputerBatchByIds")
    public Result<Object> deleteElectronicBatchByIds(@RequestParam("ids") String ids, HttpServletRequest request){
        // 校验token并获取用户信息
        AdminUserModel user = validateTokenAndGetUser(request);
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return computerService.deleteComputerBatchByIds(ids);
    }
    // 校验token并获取用户信息的方法
    private AdminUserModel validateTokenAndGetUser(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            throw new IllegalArgumentException("token不存在");
        }
        String userId = TokenUtils.verify(token);
        return adminUserService.getUserByUserId(Integer.parseInt(userId));
    }
}
